<?php
$caption = BUtils::T('lunch_offer_lbl');
$this->pageTitle = Yii::app()->name . ' - ' . $caption;

$edit_btn = BUtils::T('edit_btn');
$edit_profile_btn = BUtils::T('edit_profile_btn');

//get url_logo
$url_logo = empty($model->url_logo) ? Yii::app()->request->baseUrl . '/images/logo_default.png' : $model->url_logo;

//set $full_lanlng
$full_lanlng = '';
if (!empty($model->latitude) and !empty($model->longitude)) {
	$full_lanlng = "[{$model->latitude}, {$model->longitude}]";
}

//get week=?
$request_week_num = Yii::app()->request->getParam('week', date('N'));
$today_week_num = date('N');
$inc_days = $request_week_num - $today_week_num;
$visit_date_ts = mktime(0, 0, 0, date('m'), date('d') + $inc_days);
$visit_date = date('d.m.Y', $visit_date_ts);

$visit_week = date('l', $visit_date_ts);
$visit_week_html = BUtils::T(strtolower($visit_week) . '_lbl');

//create calendar for week days
$html_week_days_link = '';
for ($i = 1; $i < 6; $i ++) {
	if ($request_week_num == $i) {
		$url = 'javascript:;';
		$style = " w{$i} active";
	} else {
		$url = Yii::app()->createUrl($this->id.'/'.$this->action->id, array('week'=>$i));
		$style = " w{$i}";
	}
	//set date timestamp
	$diff = $i - $today_week_num;
	$date_ts = mktime(0, 0, 0, date('m'), date('d') + $diff);
	$html_week_days_link .= '<div class="calendar'.$style.'">
<div class="date">
	<a href="'.$url.'">'.date('M j', $date_ts).'</a>
</div>
<div class="week">
	<a href="'.$url.'">'.date('D', $date_ts).'</a>
</div>
</div>';
}

//define array
$aWeekDaysShort = BUtils::WeekDayKeys(7, true);
$aOpenningHours = BUtils::OpenningHours();
$aLunchHourRange = BUtils::LunchHourRange();

//set open hours
$hours_list = '';
if (!empty($hours)) {
	//set open hours display style
	$sWeekBuffer = array();
	foreach ($hours as $v) {
		$sWeekday = $aWeekDaysShort[$v['week_no']];
		$sHours = $aOpenningHours[$v['open_hour']] . ' - ' . $aOpenningHours[$v['close_hour']];
		$sWeekBuffer[$sHours][] = $v['week_no'];//add the current week day
	}
	foreach ($sWeekBuffer as $key => $v) {
		$size = count($v);
		if ($size > 1) $sWeekday = $aWeekDaysShort[$v[0]] . ' - ' . $aWeekDaysShort[$v[$size-1]];
		else $sWeekday = $aWeekDaysShort[$v[0]];
		$hours_list .= '<div class="item"><span class="weekday">'.$sWeekday.'</span><span class="hours">'.$key.'</span></div>';
	}
}
?>
<div class="container prepend-top append-bottom">
	
	<div class="span-13">
		<div class="box_white">
			<div class="biz-form">
				<h3><?php echo $caption; ?></h3>
				<div class="tips"></div>
				<div class="merchant-field">
					<div class="fieldswrap">
						<div class="calendar-fields" style="padding-left:20px;">
							<?php echo $html_week_days_link; ?>
							<div class="clear"></div>
						</div>
					</div>
					<div class="fieldswrap">
						<div class="caption-line relative">
							<div class="fixed-title">
								<div>
									<div class="span-4"><?php echo ($inc_days===0) ? BUtils::T('today_special_lbl') : BUtils::T('special_lbl'); ?></div>
									<div class="left">
										<span class="space-1"><?php echo $visit_date; ?></span>
										<span class="space-1"><?php echo $visit_week_html; ?></span>
									</div>
									<div class="clear"></div>
								</div>
							</div>
							<div class="corner-tr edit-button">
								<a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/special'); ?>"><span><?php echo $edit_btn; ?></span></a>
							</div>
						</div>
						<div>
<?php if (count($special_rows) > 0) { ?>
	<table class="va-top" id="special-list">
<?php foreach ($special_rows as $k => $row) {
	// valid time
	$selected_hour_range_html = BUtils::GetLunchHourRange($row['start_hour'], $row['end_hour']);
	$selected_hour_range_html = empty($selected_hour_range_html) ? $aLunchHourRange[1] : $selected_hour_range_html;
	
	// week days
	$selected_week_days_html = SpecialForm::HtmlWeekdays($row['week_days'], $aWeekDaysShort);
	
	//valida date
	$selected_valid_date_html = BUtils::GetDateRangeString($row['uts_start_date'], $row['uts_end_date'], 'd.m');
	
	//discount
	$discount_text = null;
	if (!empty($row['discount'])) {
		$discount_text = '-' . round($row['discount']) . '%';
	}
	
	//title
	$title_text = BUtils::T('special_auto_title_lbl', array(
		'{merchant_name}' => Yii::app()->user->getState('name'),
		'{price}' => Yii::app()->locale->numberFormatter->formatCurrency($row['price'], 'EUR'),
		'{value}' => Yii::app()->locale->numberFormatter->formatCurrency($row['value'], 'EUR'),
	));
?>
<tr>
	<td class="discount-field">
		<div class="discount-box preview-btn">
			<span class="special-discount"><?php echo $discount_text; ?></span>
		</div>
	</td>
	<td class="title-field">
		<div class="title"><?php echo CHtml::link($title_text, Yii::app()->createUrl('/special/preview', array('id'=>$row['id'])), array('class'=>'preview-btn')); ?></div>
	</td>
	<td class="period-field">
		<div class="time-range-bar">
			<div class="time-range"><?php echo $selected_hour_range_html; ?></div>
		</div>
		<div class="week-range-bar">
			<?php echo $selected_week_days_html; ?>
		</div>
		<div class="date-range-bar">
			<?php echo $selected_valid_date_html; ?>
		</div>
	</td>
</tr>
<?php
}
?>
	</table>
<?php } ?>
						</div>
					</div>
					<div class="fieldswrap">
						<div class="caption-line relative">
							<div class="fixed-title">
								<div>
									<div class="span-4"><?php echo ($inc_days===0) ? BUtils::T('today_lunch_menu_lbl') : BUtils::T('lunch_menu_lbl'); ?></div>
									<div class="left">
										<span class="space-1"><?php echo $visit_date; ?></span>
										<span class="space-1"><?php echo $visit_week_html; ?></span>
									</div>
									<div class="clear"></div>
								</div>
							</div>
							<div class="corner-tr edit-button">
								<a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/lunch/menu'); ?>" title="Edit this menu"><span><?php echo $edit_btn; ?></span></a>
							</div>
						</div>
						<div><!-- HERE is the HTML or Picture view -->
<?php
if (count($menu_rows) > 0) {
	//echo '<ul id="merchant-menu">';
	foreach ($menu_rows as $menu) {
		echo '<div class="menu-pictures">';
		echo '<a class="group1" href="'.$menu['url_picture'].'">';
		echo CHtml::image($menu['url_picture'], $menu['id'], array('width'=>'100%', 'title'=>$menu['title']));
		echo '</a>';
		echo '</div>';
	}
	//echo '</ul>';
} ?>
						</div>
					</div>
				</div>
				<div class="clear"></div>
			</div>
		</div>
	</div>
	
	<div class="span-11 right last">
		<div class="box_white">
			<div class="biz-form">
				<h3><?php echo $model->name; ?></h3>
				<div style="text-align:right;margin-right:7px;">
					<a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/merchant/profile'); ?>" title="<?php echo $edit_profile_btn; ?>"><span><?php echo $edit_profile_btn; ?></span></a>
				</div>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="span-3"><a class="pic"><img src="<?php echo $url_logo; ?>" width="100" /></a></div>
						<div class="span-5 last shop">
							<input type="hidden" id="geo-auto-location" />
							<div class="address"><?php echo $model->address . ', ' . $model->postcode . ' ' . $model->city; ?></div>
							<div class="phone"><?php echo $model->phone; ?></div>
							<div class="link"><?php echo $model->url_site; ?></div>
							<?php if ($model->average_price > 0) { ?>
							<div class="average"><?php echo BUtils::T('average_price_lbl') . ': ' . Yii::app()->locale->numberFormatter->formatCurrency($model->average_price, 'EUR'); ?></div>
							<?php } ?>
						</div>
						<div class="clear"></div>
					</div>
					<div class="fields mobile">
						<div id="merchant-map" class="map-fields">
							<img src="<?php echo Yii::app()->baseUrl; ?>/images/loader-bar.gif" />
						</div>
					</div>
				</div>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('opening_hours_lbl'); ?></div>
						<div class="added-hours"><?php echo $hours_list; ?></div>
					</div>
				</div>
				<div class="fieldswrap relative">
					<div style="position:absolute; right:5px; top:5px;">
						<a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/merchant/feature'); ?>"><span><?php echo $edit_btn; ?></span></a>
					</div>
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('detail_features_lbl'); ?></div>
						<div class="tags">
<?php
if (!empty($tags)) {
	foreach ($tags as $key => $value) {
		if (!empty($value)) {
			$string = null;
			$array = explode(',', $value);
			foreach ($array as $item) {
				$string .= '<span class="nature-list">' . BUtils::T($item) . '</span>';
			}
			echo '<div style="font-weight:bold;margin-top:10px;">'.BUtils::T($key . '_lbl').':</div><div style="margin-left:40px;">'.$string.'</div>';
		}
	}
}
?>
						</div>
					</div>
				</div>
<?php if (!empty($model->brief)) { ?>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('merchant_brief_lbl'); ?></div>
						<div class="opening">
							<p><?php echo nl2br($model->brief); ?></p>
						</div>
					</div>
				</div>
<?php } ?>
			</div>
		</div>
	</div>
</div>
<?php
//get the latLng
if (empty($model->latitude) or empty($model->longitude)) {
	$latitude = 'null';
	$longitude = 'null';
	$address = "'{$model->address}'";
} else {
	$latitude = $model->latitude;
	$longitude = $model->longitude;
	$address = 'null';
}
?>
<style type="text/css">
.map-fields{
	margin: 0px auto;
	width: 100%;
	text-align: center;
	vertical-align: middle;
}
</style>
<script type="text/javascript">
$(function() {
//load gMap
$('#merchant-map').css('height', '200px');
var imageIcon = new google.maps.MarkerImage('<?php echo Yii::app()->baseUrl; ?>/images/markers/restaurant.png',
	new google.maps.Size(32, 37), new google.maps.Point(0,0), new google.maps.Point(16,37));
var imageShadow = new google.maps.MarkerImage('<?php echo Yii::app()->baseUrl; ?>/images/markers/marker_shadow.png',
	new google.maps.Size(51, 37), new google.maps.Point(0,0), new google.maps.Point(16,37));
$("#geo-auto-location").geocomplete({
	map: '#merchant-map',
	<?php if(!empty($full_lanlng)) echo "location: {$full_lanlng},"; ?>
	markerOptions: {
		icon: imageIcon,
		shadow: imageShadow
	}
});

//open a menu picture
$('.menu-pictures a').fancybox({autoScale:false,transitionIn:'elastic',transitionOut:'elastic'});

//open a special box
$('.preview-btn').facebox({
	loadingImage: '<?php echo Yii::app()->baseUrl ?>/images/loader-bar.gif',
	closeImage: '<?php echo Yii::app()->baseUrl ?>/images/btn-close.png',
	overlayClose:false,
	width: 360
});
});
</script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&language=<?php echo Yii::app()->language; ?>"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/labs/jquery.geocomplete.min.js"></script>
